package com.zhuangxiaoyan.spring.srcode.transaction;

import com.zhuangxiaoyan.spring.srcode.service.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;

/**
 * @Classname TransactionManager
 * @Description TODO
 * @Date 2022/5/1 21:49
 * @Created by xjl
 */
public class TransactionManager {

    @Autowired
    private PlatformTransactionManager transactionManager;

    public void testTransaction() {

        TransactionStatus status = transactionManager.getTransaction(new DefaultTransactionDefinition());
        try {
            // ....  业务代码
            transactionManager.commit(status);
        } catch (Exception e) {
            transactionManager.rollback(status);
        }
    }

    @Transactional(propagation= Propagation.REQUIRED)
    public void method(){
        Admin admin=new Admin();
    }
}
